package com.ruoyi.kenuo.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.RsaUtilClient;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.kenuo.domain.Messages;
import com.ruoyi.kenuo.service.IMessagesService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.kenuo.domain.KernelMyPatient;
import com.ruoyi.kenuo.service.IKernelMyPatientService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 我的患者Controller
 *
 * @author polo tang
 * @date 2023-09-05
 */
@RestController
@RequestMapping("/kenuo/patient")
public class KernelMyPatientController extends BaseController
{
    @Autowired
    private IKernelMyPatientService kernelMyPatientService;
    @Autowired
    private ISysUserService userService;

    @Autowired
    private IMessagesService messagesService;
    /**
     *
     *
     * 查询我的患者列表
     */
    //@PreAuthorize("@ss.hasPermi('kenuo:patient:list')")
    @GetMapping("/list")
    public TableDataInfo list(KernelMyPatient kernelMyPatient) throws Exception {
        String clientUname = kernelMyPatient.getClientUname();
        String doctorUname = kernelMyPatient.getDoctorUname();
        //加密传输的解密
        if(StringUtils.isNotEmpty(clientUname)){
            clientUname = RsaUtilClient.decryptByPrivateKeyTwo("",clientUname);
            kernelMyPatient.setClientUname(clientUname);
        }
        if(StringUtils.isNotEmpty(doctorUname)){
            doctorUname = RsaUtilClient.decryptByPrivateKeyTwo("",doctorUname);
            kernelMyPatient.setDoctorUname(doctorUname);
        }

        startPage();
        List<KernelMyPatient> list = kernelMyPatientService.selectKernelMyPatientList(kernelMyPatient);
        if(!list.isEmpty()){
            list.stream().forEach(x->{
                String cname = x.getClientUname();
                String dname = x.getDoctorUname();
                SysUser clientuser = userService.getUserByUserName(cname);
                SysUser docuser = userService.getUserByUserName(dname);
                x.setClientUrl(clientuser.getAvatar());
                if(ObjectUtil.isNotEmpty(docuser)){
                    x.setDocUrl(docuser.getAvatar());
                    x.setDocNickName(docuser.getNickName()==null?"":docuser.getNickName());
                }
                x.setPatNickName(clientuser.getNickName()==null?"":clientuser.getNickName());
                //根据医患的账号查询 医生对患者的未读信息
                List<Messages> lms = messagesService.selectDocWeiDu(cname,dname);
                x.setDoctorWeiDu(lms.size());
                //根据医患的账号查询 患者d对医生对未读信息
                List<Messages> lmst = messagesService.selectDocWeiDuInt(cname,dname);
                x.setPatientWeiDu(lmst.size());
                //加密
                if(StringUtils.isNotEmpty(dname)){
                    try {
                        dname = RsaUtilClient.encryptByPublicKeyTwo(dname);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                    x.setDoctorUname(dname);
                }
                if(StringUtils.isNotEmpty(cname)){
                    try {
                        cname = RsaUtilClient.encryptByPublicKeyTwo(cname);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                    x.setClientUname(cname);
                }
            });
        }
        return getDataTable(list);
    }

    /**
     * 导出我的患者列表
     */
    @PreAuthorize("@ss.hasPermi('kenuo:patient:export')")
    @Log(title = "我的患者", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KernelMyPatient kernelMyPatient)
    {
        List<KernelMyPatient> list = kernelMyPatientService.selectKernelMyPatientList(kernelMyPatient);
        ExcelUtil<KernelMyPatient> util = new ExcelUtil<KernelMyPatient>(KernelMyPatient.class);
        util.exportExcel(response, list, "我的患者数据");
    }

    /**
     * 获取我的患者详细信息
     */
    //@PreAuthorize("@ss.hasPermi('kenuo:patient:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(kernelMyPatientService.selectKernelMyPatientById(id));
    }

    /**
     * 新增我的患者
     */
    //@PreAuthorize("@ss.hasPermi('kenuo:patient:add')")
    @Log(title = "我的患者", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KernelMyPatient kernelMyPatient)
    {
        return toAjax(kernelMyPatientService.insertKernelMyPatient(kernelMyPatient));
    }

    /**
     * 修改我的患者
     */
    //@PreAuthorize("@ss.hasPermi('kenuo:patient:edit')")
    @Log(title = "我的患者", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KernelMyPatient kernelMyPatient)
    {
        return toAjax(kernelMyPatientService.updateKernelMyPatient(kernelMyPatient));
    }

    /**
     * 删除我的患者
     */
    @PreAuthorize("@ss.hasPermi('kenuo:patient:remove')")
    @Log(title = "我的患者", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(kernelMyPatientService.deleteKernelMyPatientByIds(ids));
    }
    /**
     * 根据医生和患者的账号删除我的患者
     */
    @PostMapping("/removePatient")
    public AjaxResult removePatient(@RequestBody KernelMyPatient kernelMyPatient) throws Exception {
        //医生的用户名
        String duname = kernelMyPatient.getDoctorUname();
        if(StringUtils.isEmpty(duname)){
            return AjaxResult.error("doctorUname is null");
        }else{
            //加密传输的解密
            duname = RsaUtilClient.decryptByPrivateKeyTwo("",duname);
            kernelMyPatient.setDoctorUname(duname);
        }
        String cuname = kernelMyPatient.getClientUname();
        if(StringUtils.isEmpty(cuname)){
            return AjaxResult.error("clientUname is null");
        }else{
            //加密传输的解密
            cuname = RsaUtilClient.decryptByPrivateKeyTwo("",cuname);
            kernelMyPatient.setClientUname(cuname);
        }
        AjaxResult  ar = kernelMyPatientService.removePatient(kernelMyPatient);
        return ar;
    }
}
